package de.slash.jplaylistextractor.utils;

import de.slash.jplaylistextractor.common.ExceptionHandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * User: Slash
 * Date: 24.02.2012
 * Time: 16:42
 */
public class FileCopier
{
    private ExceptionHandler exceptionHandler;

    public FileCopier(ExceptionHandler pExceptionHandler)
    {
        exceptionHandler = pExceptionHandler;
    }

    public void copyFile(File in, File out) throws IOException
    {
        FileChannel inChannel = new FileInputStream(in).getChannel();
        FileChannel outChannel = new FileOutputStream(out).getChannel();

        try
        {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e)
        {
            exceptionHandler.handleException(e);
        } finally
        {
            if (inChannel != null)
            {
                inChannel.close();
            }
            if (outChannel != null)
            {
                outChannel.close();
            }
        }
    }
}
